home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / image2svg.php < prev    next >
PHP Script  |  2004-03-24  |  10KB  |  296 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2002 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Urs Gehrig <urs@circle.ch>                                  |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: image2svg.php,v 1.1 2002/05/01 21:31:37 chregu Exp $
  19.  
  20. /**
  21. * XML_image2svg - Image to SVG conversion
  22. *
  23. * The class converts images, such as of the format JPEG, PNG
  24. * and GIF to a standalone SVG representation. The image is being 
  25. * encoded by the PHP native encode_base64() function. You can use it
  26. * to get back a complete SVG file, which is based on a predefinded,
  27. * easy adaptable template file, or you can take the encoded
  28. * file as a return value, using the get() method. Due to the 
  29. * encoding by base64, the SVG files will increase approx. 30% in
  30. * size compared to the conventional image.
  31. *
  32. * NOTE: 
  33. *   Since PHP 4.0.5 GetImageSize() support has been added.
  34. *
  35. * EXAMPLE:
  36. *   $i = &new XML_image2svg("test.jpg" );
  37. *   $i->tplFile = "tpl.image.svg";
  38. *   $i->show();
  39. *
  40. *   The class allows also commandline conversion of images:
  41. *   C:\>php-cli -f Test_image2svg.php > test.svg
  42. *
  43. * REFERENCES:
  44. *   http://www.ietf.org/rfc/rfc2397.txt, The "data" URL scheme
  45. *   http://www.ietf.org/rfc/rfc2045.txt, Multipurpose Internet Mail Extensions
  46. *   http://www.w3.org/Graphics/SVG/, Scalable Vector Graphics (SVG)
  47. */
  48.  
  49. /**
  50. * Image to svg conversion.
  51. *
  52. *
  53. * @version  $Id: image2svg.php,v 1.1 2002/05/01 21:31:37 chregu Exp $
  54. * @package  XML
  55. * @author   Urs Gehrig <urs@circle.ch> 
  56. */
  57. require_once( "PEAR.php") ;
  58. require_once( "HTML/IT.php") ;    
  59.  
  60. class XML_image2svg extends PEAR {
  61.  
  62.     /**
  63.     * TODO: Modify the quality of the output image. Needs GD support.
  64.     *
  65.     * @var imageQuality
  66.     */
  67.     var $imageQuality = 0.8;
  68.     
  69.     /** 
  70.     * Image file pointer. 
  71.     *
  72.     */
  73.     var $fp = null;
  74.         
  75.     /**
  76.     * Storage representation of the image string
  77.     *
  78.     * @var  string
  79.     */
  80.     var $buffer = "";
  81.     
  82.     /**
  83.     * Image information
  84.     *
  85.     * @var  array
  86.     */
  87.     var $par = array(0, 0, 0, "" );    
  88.     
  89.     /**
  90.     * Constructs a new XML_image2svg object.
  91.     *
  92.     * @param string image filename    
  93.     * @access public
  94.     * @author Urs Gehrig <urs@circle.ch>
  95.     */
  96.     function XML_image2svg ($fileName="" )
  97.     {
  98.         $this->PEAR();
  99.         $this->fileName = $fileName;
  100.         $this->tpl      = new IntegratedTemplate("." );
  101.         $this->tplFile  = "";
  102.         
  103.         $this->_getImage();
  104.     }
  105.  
  106.     /**
  107.     * Read the image file binary-safe
  108.     *
  109.     * @access private
  110.     * @return mixed bool on success or an error object otherwise
  111.     * @author Urs Gehrig <urs@circle.ch>     
  112.     */
  113.     function _getImage ()
  114.     {        
  115.         if(file_exists($this->fileName )) {
  116.             $fp = fopen ($this->fileName, "rb" );
  117.             $this->buffer = fread($fp, filesize ($this->fileName ));
  118.             
  119.             if (!$fp) {
  120.                 return $this->errorHandler("FAILED_OPENING_FILE", __FILE__, __LINE__ );
  121.             }
  122.             
  123.             $this->fp = $fp;
  124.             fclose ($fp);
  125.              
  126.             return $this->_getImageParamteres();
  127.         }
  128.         return $this->errorHandler("FILE_NOT_FOUND", __FILE__, __LINE__ );         
  129.     }
  130.     
  131.     /**
  132.     * Get the specific image parameters
  133.     *
  134.     * @access private
  135.     * @return mixed bool on success or an error object on fail
  136.     * @author Urs Gehrig <urs@circle.ch>      
  137.     */    
  138.     function _getImageParamteres ()
  139.     {
  140.         if (is_resource($this->fp )) {
  141.             $this->par = @getimagesize($this->fileName );
  142.             SWITCH ($this->par[2] ) {
  143.                 case 1:
  144.                     $this->imagetype = 'image/gif';
  145.                     break;
  146.                 case 2:
  147.                     $this->imagetype = 'image/jpg';
  148.                     break;
  149.                 case 3:
  150.                     $this->imagetype = 'image/png';
  151.                     break;
  152.                 default:
  153.                     return $this->errorHandler("MIME_ERROR", __FILE__, __LINE__ );    
  154.             }
  155.             return TRUE;
  156.         }
  157.         return $this->errorHandler("READ_ERROR", __FILE__, __LINE__ );
  158.     }
  159.     
  160.     /**
  161.     * Convert the image to an SVG file. Having specified a template file
  162.     * allows direct inclusion of the image into a more sophisticated SVG file;
  163.     * while not giving a template file, a most slim standalone SVG string will
  164.     * be returned.
  165.     * 
  166.     * @access private
  167.     * @return mixed a string on success or an error object otherwise
  168.     * @author Urs Gehrig <urs@circle.ch>      
  169.     */    
  170.     function _convertToSvg ()
  171.     {
  172.         if(file_exists($this->tplFile ) ) {
  173.             
  174.             if(strlen($this->tplFile )) {
  175.                 $this->tpl->loadTemplatefile($this->tplFile, true, true);
  176.                 $content = array(
  177.                     "img_width"         =>  $this->par[0],
  178.                     "img_height"        =>  $this->par[1],
  179.                     "data_url_scheme"   =>  sprintf("data:%s;base64,%s", 
  180.                                                 $this->imagetype,
  181.                                                 $this->encodeImage()
  182.                                             )
  183.                 );          
  184.                 $this->tpl->setVariable($content);  
  185.                 
  186.                 return $this->tpl->get();
  187.                 
  188.             } else {
  189.                 $s .= sprintf("<?xml version=\"1.0\" ?>" );
  190.                 $s .= sprintf("<svg %s viewBox=\"0 0 %s %s\" xml:space=\"preserve\">",
  191.                             $this->par[3],
  192.                             $this->par[0],
  193.                             $this->par[1]
  194.                 );   
  195.                 $s .= sprintf("<g><image %s xlink:href=\"data:%s;base64,%s\"  /></g>", 
  196.                             $this->par[3],
  197.                             $this->imagetype,
  198.                             $this->encodeImage()
  199.                 );
  200.                 $s .= sprintf("</svg>");
  201.                 
  202.                 return $s;
  203.             }
  204.             
  205.         } else {
  206.             return $this->errorHandler("TEMPLATE_FILE_MISSING", __FILE__, __LINE__ );
  207.         }
  208.     }
  209.  
  210.     /**
  211.     * Encode image by base 64
  212.     *
  213.     * @access public
  214.     * @return string Returns encoded image as string
  215.     * @author Urs Gehrig <urs@circle.ch>      
  216.     */    
  217.     function encodeImage ()
  218.     {
  219.         return base64_encode($this->buffer );
  220.     }
  221.         
  222.     /**
  223.     * Return the SVG output
  224.     *
  225.     * @access public
  226.     * @return mixed PEAR_Error object or string
  227.     * @author Urs Gehrig <urs@circle.ch>      
  228.     */    
  229.     function get () 
  230.     {
  231.         if (is_resource($this->fp )) {
  232.             return $this->_convertToSvg();
  233.         }
  234.         return $this->errorHandler("CONVERSION", __FILE__, __LINE__ );;        
  235.     }
  236.  
  237.     /**
  238.     * Display the SVG output
  239.     *
  240.     * @access public
  241.     * @return mixed PEAR_Error object or bool on success
  242.     * @author Urs Gehrig <urs@circle.ch>     
  243.     */
  244.     function show () 
  245.     {
  246.         if ($res = $this->get() ) {
  247.             header("Content-type: image/svg+xml" );
  248.             echo $res;
  249.             return TRUE;
  250.         }
  251.         return $this->errorHandler("UNKNOWN", __FILE__, __LINE__ );
  252.     }
  253.     
  254.     /**
  255.     * Handling the error messages
  256.     *
  257.     * @param string error message
  258.     * @param string file where the error occured
  259.     * @param string linenumber where the error occured    
  260.     * @access public
  261.     * @return object PEAR_Error object
  262.     * @author Urs Gehrig <urs@circle.ch>     
  263.     */
  264.     function errorHandler ($err="UNKNOWN", $file=__FILE__, $line=__LINE__ )
  265.     {
  266.         $this->error_codes = array (
  267.             "FILE_NOT_FOUND"        => "The image file could not be found.",
  268.             "FAILED_OPENING_FILE"   => "The image file could not be opened.",
  269.             "READ_ERROR"            => "The image file could not be read.",
  270.             "MIME_ERROR"            => "Unsupported mimetype.",
  271.             "TEMPLATE_FILE_MISSING" => "The SVG template file is missing.",
  272.             "CONVERSION"            => "Error on returning the conversion result.",
  273.             "UNKNOWN"               => "Unknown error."
  274.         );
  275.  
  276.         return $this->raiseError(sprintf("%s [%s on line %d].", 
  277.                     $this->error_codes[$err], 
  278.                     $file, 
  279.                     $line ), 
  280.                     null,
  281.                     PEAR_ERROR_DIE 
  282.                );    
  283.     }
  284.     
  285.     /**
  286.     * Class destructor
  287.     *
  288.     * @access private
  289.     * @author Urs Gehrig <urs@circle.ch>       
  290.     */
  291.     function _XML_image2svg ()
  292.     {
  293.         $this->_PEAR();
  294.     }    
  295. }
  296. ?>